interface ToastOptions {
  title?: string
  message: string
  duration?: number
}

let toastInstance: any = null

export const useToast = () => {
  const setToastInstance = (instance: any) => {
    toastInstance = instance
  }

  const success = (options: string | ToastOptions) => {
    if (!toastInstance) {
      console.warn('Toast instance not found')
      return
    }

    const toast = typeof options === 'string' 
      ? { message: options, type: 'success' as const, duration: 3000 }
      : { ...options, type: 'success' as const, duration: options.duration || 3000 }
    
    toastInstance.addToast(toast)
  }

  const error = (options: string | ToastOptions) => {
    if (!toastInstance) {
      console.warn('Toast instance not found')
      return
    }

    const toast = typeof options === 'string'
      ? { message: options, type: 'error' as const, duration: 4000 }
      : { ...options, type: 'error' as const, duration: options.duration || 4000 }
    
    toastInstance.addToast(toast)
  }

  const warning = (options: string | ToastOptions) => {
    if (!toastInstance) {
      console.warn('Toast instance not found')
      return
    }

    const toast = typeof options === 'string'
      ? { message: options, type: 'warning' as const, duration: 3500 }
      : { ...options, type: 'warning' as const, duration: options.duration || 3500 }
    
    toastInstance.addToast(toast)
  }

  const info = (options: string | ToastOptions) => {
    if (!toastInstance) {
      console.warn('Toast instance not found')
      return
    }

    const toast = typeof options === 'string'
      ? { message: options, type: 'info' as const, duration: 3000 }
      : { ...options, type: 'info' as const, duration: options.duration || 3000 }
    
    toastInstance.addToast(toast)
  }

  return {
    setToastInstance,
    success,
    error,
    warning,
    info,
  }
}

